package xyz.jcat.mybatis.leaf;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import xyz.jcat.common.enums.IdType;
import xyz.jcat.leaf.LeafIdWorker;

@Component
public class LeafIdGenerator implements IdentifierGenerator {

    @Value("${idWorker.mybatis.key}")
    private String key;
    @Value("${idWorker.mybatis.idType:long}")
    private String idType;
    @Autowired
    private LeafIdWorker leafIdWorker;

    public Number nextId(Object entity) {
        Long id = leafIdWorker.nextId(key);
        if(IdType.isInt(idType)) {
            return id.intValue();
        }
        return id;
    }

}
